import { Context } from "koa";
import { dealDbMessage, uploadFile } from "../utils/index";

class UploadCtl {
  static async uploadImg(ctx: Context) {
    let file;
    const { request } = ctx;
    const { body, query } = request;
    if (ctx.request.files) {
      file = ctx.request.files.file;
      const path = (query.path as string) || "path";
      const res = await uploadFile(file, path);
      if (res) {
        ctx.body = dealDbMessage(res);
        return;
      }
    }
    ctx.body = dealDbMessage(null, 500, "上传照片失败");
  }
}

export default UploadCtl;
